{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[41mThread-26 拿到A锁\u001b[0m\n",
      "\u001b[41mThread-26 拿到B锁\u001b[0m\n",
      "\u001b[41mThread-26 拿到B锁\u001b[0m\n",
      "\u001b[41mThread-27 拿到A锁\u001b[0m\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'Mythread' object has no attribute 'clear'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-72083e2d54b9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     31\u001b[0m         \u001b[0mt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mMythread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     32\u001b[0m         \u001b[0mt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstart\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 33\u001b[1;33m     \u001b[0mt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclear\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     34\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     35\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Mythread' object has no attribute 'clear'"
     ]
    }
   ],
   "source": [
    "from threading import Thread,Lock\n",
    "import time\n",
    "a=Lock()\n",
    "b=Lock()\n",
    "class Mythread(Thread):\n",
    "    def run(self):\n",
    "        self.fun1()\n",
    "        self.fun2()\n",
    "    def fun1(self):\n",
    "        a.acquire()\n",
    "        print('\\033[41m%s 拿到A锁\\033[0m'%self.name)\n",
    "        \n",
    "        b.acquire()\n",
    "        print('\\033[41m%s 拿到B锁\\033[0m'%self.name)\n",
    "        b.release()\n",
    "        \n",
    "        a.release()\n",
    "    def fun2(self):\n",
    "        b.acquire()\n",
    "        print('\\033[41m%s 拿到B锁\\033[0m'%self.name)\n",
    "        \n",
    "        time.sleep(2)\n",
    "        \n",
    "        a.acquire()\n",
    "        print('\\033[41m%s 拿到A锁\\033[0m'%self.name)\n",
    "        a.release()\n",
    "        b.release()\n",
    "if __name__=='__main__':\n",
    "    \n",
    "    for i in range(10):\n",
    "        t=Mythread()\n",
    "        t.start()\n",
    "    \n",
    "            \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thread-36 get sm\n",
      "Thread-37 get sm\n",
      "Thread-38 get sm\n",
      "Thread-39 get sm\n",
      "Thread-40 get sm\n",
      "Thread-41 get smThread-43 get sm\n",
      "Thread-44 get smThread-42 get sm\n",
      "\n",
      "Thread-45 get sm\n",
      "\n",
      "Thread-46 get sm\n",
      "Thread-48 get sm\n",
      "Thread-47 get sm\n",
      "Thread-50 get sm\n",
      "Thread-49 get sm\n",
      "Thread-51 get smThread-52 get sm\n",
      "\n",
      "Thread-53 get sm\n",
      "Thread-54 get sm\n",
      "Thread-55 get sm\n",
      "Thread-56 get sm\n",
      "Thread-58 get sm\n",
      "Thread-57 get sm\n"
     ]
    }
   ],
   "source": [
    "from threading import Thread,Semaphore\n",
    "import threading\n",
    "import time\n",
    "def func():\n",
    "    sm.acquire()\n",
    "    print('%s get sm'%threading.current_thread().getName())\n",
    "    time.sleep(3)\n",
    "    sm.release()\n",
    "if __name__=='__main__':\n",
    "    sm=Semaphore(5)\n",
    "    for i in range(23):\n",
    "        t=Thread(target=func)\n",
    "        t.start()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-7-12e84e1c434c>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-7-12e84e1c434c>\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m    while no event_is_set():\u001b[0m\n\u001b[1;37m                        ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "from threading import Thread,Event\n",
    "import threading\n",
    "import time,random\n",
    "def coon_mysql():\n",
    "    count=1\n",
    "    while no event.is_set():\n",
    "        if count>3\n",
    "            raise TimeoutError('链接超时')\n",
    "        print('<%s>第%s次尝试连接'%(threading.current_thread().getNmae(),count()))\n",
    "        event.wait(0.5)\n",
    "        count+=1\n",
    "    print('<%s>连接成功'%(threading.current_thread().getNmae()))\n",
    "    \n",
    "def check_mysql():\n",
    "    print('\\033[45m[%s]正在检查mysql\\033[0m'%threading.current_thread().getName())\n",
    "    time.sleep(random.randint(2,4)\n",
    "    event.set()\n",
    "if __name__=='__main__':\n",
    "    event=Event()\n",
    "               \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Thread-65>第1次尝试连接\n",
      "<Thread-66>第1次尝试连接\n",
      "\u001b[45m[Thread-67]正在检查mysql\u001b[0m\n",
      "<Thread-65>第2次尝试连接\n",
      "<Thread-66>第2次尝试连接\n",
      "<Thread-65>第3次尝试连接\n",
      "<Thread-66>第3次尝试连接\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread Thread-65:\n",
      "Traceback (most recent call last):\n",
      "  File \"D:\\anaconda3\\lib\\threading.py\", line 917, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"D:\\anaconda3\\lib\\threading.py\", line 865, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"<ipython-input-23-e77cda201737>\", line 9, in coon_mysql\n",
      "    raise TimeoutError('链接超时')\n",
      "TimeoutError: 链接超时\n",
      "\n",
      "Exception in thread Thread-66:\n",
      "Traceback (most recent call last):\n",
      "  File \"D:\\anaconda3\\lib\\threading.py\", line 917, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"D:\\anaconda3\\lib\\threading.py\", line 865, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"<ipython-input-23-e77cda201737>\", line 9, in coon_mysql\n",
      "    raise TimeoutError('链接超时')\n",
      "TimeoutError: 链接超时\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from threading import Thread,Event\n",
    "import threading\n",
    "import time,random\n",
    "def coon_mysql():\n",
    "    \n",
    "    count=1\n",
    "    while not event.is_set():\n",
    "        if count>3:\n",
    "            raise TimeoutError('链接超时')\n",
    "        print('<%s>第%s次尝试连接'%(threading.current_thread().getName(),count))\n",
    "        event.wait(0.5)\n",
    "        count+=1\n",
    "    print('<%s>连接成功'%(threading.current_thread().getNmae()))\n",
    "    \n",
    "def check_mysql():\n",
    "    \n",
    "    print('\\033[45m[%s]正在检查mysql\\033[0m'%threading.current_thread().getName())\n",
    "    time.sleep(random.randint(2,4))\n",
    "    event.set()\n",
    "if __name__=='__main__':\n",
    "    event=Event()\n",
    "    coon1=Thread(target=coon_mysql)\n",
    "    coon2=Thread(target=coon_mysql)\n",
    "    check=Thread(target=check_mysql)\n",
    "    coon1.start()\n",
    "    coon2.start()\n",
    "    check.start()  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<<<<45\n",
      "run the thread:6\n",
      "run the thread:5\n",
      "run the thread:2\n",
      "run the thread:1\n",
      "run the thread:3\n",
      "run the thread:4\n",
      "run the thread:8\n",
      "run the thread:9\n",
      "run the thread:7\n",
      "run the thread:0\n",
      "<<<<q\n"
     ]
    }
   ],
   "source": [
    "import threading \n",
    "\n",
    "def run(n):\n",
    "    con.acquire()\n",
    "    con.wait()\n",
    "    print('run the thread:%s'%n)\n",
    "    con.release()\n",
    "if __name__=='__main__':\n",
    "    con=threading.Condition()\n",
    "    for i in range(10):\n",
    "        t=threading.Thread(target=run,args=(i,))\n",
    "        t.start()\n",
    "    while True:\n",
    "        inp=input('<<<<')\n",
    "        if inp=='q':\n",
    "            break\n",
    "        con.acquire()\n",
    "        con.notify(int(inp))\n",
    "        con.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "from threading import Timer\n",
    "def hello():\n",
    "    print('hello world')\n",
    "t=Timer(3,hello)\n",
    "t.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fujun\n",
      "fuwen\n"
     ]
    }
   ],
   "source": [
    "import queue\n",
    "q=queue.Queue()\n",
    "q.put('fujun')\n",
    "q.put('fuwen')\n",
    "print(q.get())\n",
    "print(q.get())\n",
    "print(q.get())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
